const sqlite = require('./sqlite');
class QxDao {

  constructor(file) {
    this.file = file;
    this.db = new sqlite.SqliteDb(file);
    this.init();
  }

  init() {
    let cityrankSQL = "create table if not exists cityrank(id TEXT, dt TEXT,name TEXT, type TEXT, date TEXT, content BLOB)";
    let historyCurveSQL = "create table if not exists historyCurve(id TEXT, dt TEXT, name TEXT, type TEXT, startDate TEXT, endDate TEXT, content BLOB)";

    this.db.createTable(cityrankSQL);
    this.db.createTable(historyCurveSQL);

    return this;
  }

  insertCityrank(id, dt, name, type, date, content) {
    let insertSql = "insert into cityrank(id, dt, name, type, date, content) values(?,?,?,?,?,?)";
    this.db.insertData(insertSql, [
      [id, dt, name, type, date, content]
    ]);
  }

  insertCityranks(cityranks) {
    let insertSql = "insert into cityrank(id, dt, name, type, date, content) values(?,?,?,?,?,?)";
    this.db.insertData(insertSql, cityranks);
  }

  insertProvincerank(id, dt, name, type, date, content) {
    let insertSql = "insert into provincerank(id, dt, name, type, date, content) values(?,?,?,?,?,?)";
    this.db.insertData(insertSql, [
      [id, dt, name, type, date, content]
    ]);
  }

  insertProvinceranks(provinceranks) {
    let insertSql = "insert into provincerank(id, dt, name, type, date, content) values(?,?,?,?,?,?)";
    this.db.insertData(insertSql, provinceranks);
  }

  insertHistory(id, dt, name, type, start, end, content) {
    let insertSql = "insert into historyCurve(id, dt, name, type, startDate, endDate, content) values(?,?,?,?,?,?,?)";
    this.db.insertData(insertSql, [
      [id, dt, name, type, start, end, content]
    ]);
  }

  insertHistories(histories) {
    let insertSql = "insert into historyCurve(id, dt, name, type, startDate, endDate, content) values(?,?,?,?,?,?,?)";
    this.db.insertData(insertSql, histories);
  }

  close() {
    this.db.close();
  }
}

exports.QxDao = QxDao;